import 'package:dio/dio.dart' as dio;
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../global/dnp_global_manager.dart';
import '../../../manager/dnp_plugin_manager.dart';
import '../../../net/dnp_net_manager.dart';
import '../../register/dnp_register_page.dart';
import '../../widget/dnp_marquee_widget.dart';
import '../model/dnp_home_data_model.dart';
import '../model/dnp_home_rolled_data_model.dart';
import 'dnp_home_heard_mixin.dart';

/// 小卡位UI mixin
mixin DnpHomeSmallCardMixin implements DnpHomeHeardMixin {
  DnpCitizen? _syllable;
  List<String> marquee = [];
  final _products = <DnpCitizen>[].obs;

  void initSmallCardData(DnpHomeDataModel model) {
    _syllable = getCitizen(model,"oc");
    _products.value = getProducts(model);
    marquee = model.mumbled ?? [];
  }

  Widget buildSmallCard(BuildContext context, DnpHomeDataModel model) {
    initSmallCardData(model);
    return CustomScrollView(
      physics: const BouncingScrollPhysics(),
      slivers: [
        SliverToBoxAdapter(child: const SizedBox(height: 42)),
        SliverToBoxAdapter(child: _buildSmallCardHorn()),
        SliverToBoxAdapter(child: const SizedBox(height: 14)),
        SliverToBoxAdapter(child: _buildSmallCardCenter(context)),
        SliverToBoxAdapter(child: const SizedBox(height: 14)),
        SliverToBoxAdapter(child: _buildSmallCardListTitle()),
        _buildSmallCardList(),
      ],
    );
  }

  Widget _buildSmallCardHorn() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.symmetric(horizontal: 12, vertical: 6),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20),
        border: Border.all(color: Color(0xFF1D4AC8), width: 1),
      ),
      child: Row(
        children: [
          Image.asset(
            "assets/images/ic_small_horn_icon.png",
            width: 20,
            height: 16,
          ),
          SizedBox(width: 6),
          Container(
            width: 1,
            height: 16,
            color: Colors.grey,
          ),
          SizedBox(width: 6),
          Expanded(
              child: DnpMarqueeWidget(
            messages: marquee,
            duration: 3,
            animationDuration: 500,
            height: 16,
            textStyle: const TextStyle(
              fontSize: 12,
              color: Colors.black87,
              height: 1.0,
            ),
            alignment: Alignment.centerLeft,
          )),
        ],
      ),
    );
  }

  Widget _buildSmallCardCenter(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        onToApply(context, "${_syllable?.staring}");
      },
      child: Container(
          width: double.infinity,
          height: 247,
          decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage("assets/images/ic_small_center_bg.webp"),
                fit: BoxFit.fill),
          ),
          child: Column(children: [
            SizedBox(height: 19),
            Text(
              _syllable?.later ?? "",
              style: TextStyle(fontSize: 14, color: Color(0xFFC1C1C1)),
            ),
            SizedBox(height: 10),
            _buildSmallCardCenterMoney(),
            SizedBox(height: 12),
            _buildSmallCardCenterButton(),
            SizedBox(height: 20),
            _buildSmallCardCenterApply(context),
          ])),
    );
  }

  Widget _buildSmallCardCenterMoney() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          _syllable?.week ?? "",
          style: TextStyle(
              fontSize: 48,
              color: Color(0xFF252630),
              fontWeight: FontWeight.w900),
        ),
      ],
    );
  }

  Widget _buildSmallCardCenterButton() {
    return Row(
      children: [
        SizedBox(width: 20),
        _buildSmallCardCenterButtonItem(
          _syllable?.three ?? "",
          _syllable?.hours ?? "",
        ),
        SizedBox(width: 12),
        _buildSmallCardCenterButtonItem(
          _syllable?.stayed ?? "",
          _syllable?.custom ?? "",
        ),
        SizedBox(width: 20),
      ],
    );
  }

  Widget _buildSmallCardCenterButtonItem(String title, String content) {
    return Expanded(
        child: Container(
      height: 56,
      width: double.infinity,
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(color: Color(0xFF0193FE), width: 1),
        borderRadius: BorderRadius.circular(56),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            title,
            style: TextStyle(fontSize: 14, color: Color(0xFF252630)),
          ),
          Text(
            content,
            style: TextStyle(
                fontSize: 16,
                color: Color(0xFF252630),
                fontWeight: FontWeight.w600),
          ),
        ],
      ),
    ));
  }

  Widget _buildSmallCardCenterApply(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          _syllable?.earnestly ?? '',
          style: TextStyle(
              fontWeight: FontWeight.w900, fontSize: 22, color: Colors.white),
        ),
        SizedBox(width: 10),
        Image.asset(
          "assets/images/ic_small_center_arrow.png",
          width: 24,
          height: 24,
        ),
      ],
    );
  }

  Widget _buildSmallCardListTitle() {
    return Text(
      "Loan supermarket",
      style: TextStyle(
          fontSize: 16, fontWeight: FontWeight.w600, color: Color(0xFF252630)),
    );
  }

  Widget _buildSmallCardList() {
    return Obx(() => SliverList(
            delegate:
                SliverChildBuilderDelegate((BuildContext context, int index) {
          return _buildSmallCardListItem(context, _products[index]);
        }, childCount: _products.value.length)));
  }

  Widget _buildSmallCardListItem(BuildContext context, DnpCitizen model) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        onToApply(context, "${model.staring}");
      },
      child: Container(
        margin: EdgeInsets.only(top: 12),
        padding: EdgeInsets.symmetric(horizontal: 14, vertical: 10),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(20),
          color: Colors.white,
        ),
        child: Column(
          children: [
            Row(
              children: [
                Image.network(
                  model.griesbach ?? "",
                  width: 28,
                  height: 28,
                ),
                SizedBox(width: 6),
                Text(
                  model.tossing ?? "",
                  style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: Color(0xFF252630)),
                ),
                Spacer(),
                Container(
                  width: 90,
                  height: 36,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: Color(0xFF1D4AC8),
                    borderRadius: BorderRadius.circular(18),
                  ),
                  child: Text(
                    model.earnestly ?? "",
                    style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.w500,
                        color: Colors.white),
                  ),
                ),
              ],
            ),
            SizedBox(height: 12),
            _buildSmallCardListItemChild(model)
          ],
        ),
      ),
    );
  }

  Widget _buildSmallCardListItemChild(DnpCitizen model) {
    return Container(
      decoration: BoxDecoration(
        color: Color(0xFFE9F5FF),
        borderRadius: BorderRadius.circular(16),
      ),
      padding: EdgeInsets.symmetric(horizontal: 18, vertical: 14),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          _buildSmallCardListBottomItem(
              model.week ?? "",
              TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                  color: Color(0xFF1D4AC8)),
              model.later ?? "",
              TextStyle(
                  fontSize: 12,
                  fontWeight: FontWeight.w400,
                  color: Color(0xFF999999))),
          _buildSmallCardListBottomItem(
              model.hours ?? "",
              TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w400,
                  color: Color(0xFF333333)),
              model.three ?? "",
              TextStyle(
                  fontSize: 12,
                  fontWeight: FontWeight.w400,
                  color: Color(0xFF999999))),
          _buildSmallCardListBottomItem(
              model.struck ?? "",
              TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w400,
                  color: Color(0xFF333333)),
              model.stayed ?? "",
              TextStyle(
                  fontSize: 12,
                  fontWeight: FontWeight.w400,
                  color: Color(0xFF999999))),
        ],
      ),
    );
  }

  Widget _buildSmallCardListBottomItem(String title, TextStyle titleStyle,
      String subTitle, TextStyle subTitleStyle) {
    return Expanded(
        child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
      Text(
        title,
        style: titleStyle,
      ),
      Text(
        subTitle,
        style: subTitleStyle,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      )
    ]));
  }

  @override
  String get heard => "oc";

  void onToApply(BuildContext context, String already) {
    bool isRegister = DnpGlobalManager().isRegister;

    if (isRegister == false) {
      Navigator.of(context)
          .push(MaterialPageRoute(builder: (_) => DnpRegisterPage()));
      return;
    }
    DnpPluginManager().showLoading();
    DnpNetManager()
        .post("/haseye/rolled",
            data: dio.FormData.fromMap({'already': already}))
        .then((value) {
      DnpPluginManager().hideLoading();
      if (value.success == true) {
        DnpHomeRolledDataModel model =
            DnpHomeRolledDataModel.fromJson(value.close);
        handleJump(model.belated ?? "",_syllable?.tossing ?? "");
      } else {
        DnpPluginManager().showToast(value.kurios ?? "");
      }
    });
  }
}
